/*
 *
 * @ProjectName:    my_umi_app
 * @Package:
 * @ClassName:      list
 * @Description:    JSX文件
 * @Author:         SuPeiSen
 * @CreateDate:     2021/2/25 3:29 下午
 * @UpdateDate:     2021/2/25 3:29 下午
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */

import ProTable, { ProColumns } from '@ant-design/pro-table';
import { ShareUrlItem } from '@/type/share_url_type';
import { message, Switch, Tag } from 'antd';
import { history } from '@@/core/history';
import { numberToBool } from '@/utils/app_fun';
import {
  DeleteShareUrlApi,
  GetShareUrlListApi,
  UpdateShareUrlStatusApi,
} from '@/service/share_url_api';
import { SortOrder } from 'antd/lib/table/interface';
import React from 'react';
import { updateCategoryApi } from '@/service/category_api';
import {
  GetAdListApi,
  UpdateAdApi,
  UpdateAdStatusByIdApi,
} from '@/service/ad_api';
import { AdItemType } from '@/type/ad_type';
import { AppConfig, AppImageUrl } from '@/config/app_config';

const columns: ProColumns<AdItemType>[] = [
  {
    title: 'id',
    dataIndex: 'id',
    key: 'id',
    search: false,
    editable: false,
  },
  {
    title: '广告名称',
    dataIndex: 'ad_title',
    editable: false,
    render: (dom, item) => {
      return <i style={{ fontWeight: 'bold' }}>{item.ad_title}</i>;
    },
  },
  {
    title: '广告链接',
    dataIndex: 'url_path',
    editable: false,
    render: (dom, item) => {
      return (
        <i
          onClick={() => {
            window.open(item.ad_url);
          }}
          style={{ color: 'blue', textDecoration: 'underline' }}
        >
          {item.ad_url}
        </i>
      );
    },
  },
  {
    title: '文章图片',
    dataIndex: 'article_image',
    render: (dom, item) => {
      return (
        <img
          alt={item.ad_image}
          src={AppImageUrl(item.ad_image)}
          width="150px"
        />
      );
    },
  },
  {
    title: '邮箱',
    dataIndex: 'ad_email',
    editable: false,
    render: (dom, item) => {
      return <Tag>{item.ad_email}</Tag>;
    },
  },
  {
    title: '排序',
    dataIndex: 'px',
    editable: false,
    render: (dom, item) => {
      return <Tag>{item.px}</Tag>;
    },
  },
  {
    title: '添加时间',
    dataIndex: 'add_time',
  },
  {
    title: '点击次数',
    dataIndex: 'ad_click_num',
  },
  {
    title: '备注',
    dataIndex: 'remarks',
  },
  {
    title: '操作',
    valueType: 'option',
    render: (text, record, _, action) => [
      <a
        key={'editable_' + `${record.id}`}
        onClick={(date) => {
          history.push({
            pathname: '/admin/ad/update',
            query: {
              id: record.id,
            },
          });
        }}
      >
        编辑
      </a>,
      <a
        target="_blank"
        key={'delete_' + `${record.id}`}
        onClick={() => {
          deleteAdUrl(record.id, action);
        }}
      >
        删除
      </a>,

      <Switch
        key={record.id}
        checkedChildren="上架"
        unCheckedChildren="下架"
        defaultChecked={numberToBool(record.status)}
        onChange={async (checked, event) => {
          const success = await UpdateAdStatusByIdApi({
            id: record.id,
            status: checked,
          });
          if (success) {
            message.success('更新成功');
          } else {
            message.error('更新失败');
          }
        }}
      />,
    ],
  },
];

const deleteAdUrl = async (id: number, action: any) => {
  const success = await DeleteShareUrlApi(id);
  if (success) {
    action.reload();
    message.success('删除成功');
  } else {
    message.error('删除失败');
  }
};

export default () => {
  const getShareUrlList = async (
    params: {
      pageSize?: number;
      current?: number;
      keyword?: string;
    },
    sort: {
      [key: string]: SortOrder;
    },
    filter: {
      [key: string]: React.ReactText[];
    },
  ) => {
    const model = await GetAdListApi({
      page: params.current ?? 1,
      limit: params.pageSize ?? 10,
    });
    if (model.success) {
      return {
        success: true,
        data: model.data!.data.lists,
        total: model.data!.data.total,
      };
    }
    return {
      data: [],
      success: false,
    };
  };

  return (
    <ProTable<AdItemType>
      columns={columns}
      rowKey="id"
      request={getShareUrlList}
      editable={{
        onSave: async (key, row) => {
          console.log(row);
          const success = await updateCategoryApi(row);
          if (success) {
            message.success('成功');
          } else {
            message.error('失败');
          }
        },
      }}
      pagination={{ pageSize: 5 }}
    />
  );
};
